package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.JoinPeopleEntity;
import com.yimi.yinhepda.utils.Common;

import java.util.ArrayList;
import java.util.List;

/**
 * 装卸组参与人列表适配器
 *
 * @author 000432
 * @date 2018/4/28 0028
 */
public class LoadUnLoadGroupAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private List<JoinPeopleEntity> mList;

    public LoadUnLoadGroupAdapter(Context context, List<JoinPeopleEntity> list) {
        this.mContext = context;
        this.mList = list;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_load_unload_group, null);
            holder.ll_joinPeople = convertView.findViewById(R.id.ll_joinPeople);
            holder.tv_joinType = convertView.findViewById(R.id.tv_joinType);
            holder.tv_joinWorkNum = convertView.findViewById(R.id.tv_joinWorkNum);
            holder.tv_joinName = convertView.findViewById(R.id.tv_joinName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final JoinPeopleEntity entity = mList.get(position);
        if (entity.getJobName() != null && !"null".equals(entity.getJobName())) {
            holder.tv_joinType.setText(String.format("%s", entity.getJobName()).replace("分拨", ""));
        } else {
            holder.tv_joinType.setText("--");
        }

        holder.tv_joinWorkNum.setText(entity.getWorkNum());
        holder.tv_joinName.setText(entity.getUserName());

        if (entity.isChecked()) {
            holder.ll_joinPeople.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        } else {
            holder.ll_joinPeople.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isChecked()) {
                    entity.setChecked(false);
                    holder.ll_joinPeople.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    entity.setChecked(true);
                    holder.ll_joinPeople.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    static class ViewHolder {
        LinearLayout ll_joinPeople;
        /**
         * 参与人类型
         */
        TextView tv_joinType;
        /**
         * 参与人工号
         */
        TextView tv_joinWorkNum;
        /**
         * 参与人姓名
         */
        TextView tv_joinName;
    }

    public void setmList(List<JoinPeopleEntity> mList) {
        this.mList = mList;
    }

    public List<JoinPeopleEntity> getList() {
        return mList;
    }
}
